import java.util.*;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/8/14
 * Time:10:32
 **/
public class Main {
    /**
     * 给你两个数组，分别输出它的交集和并集
     */
    public static void main(String[] args) {
        int[] arr1 = {1,2,4};
        int[] arr2 = {7,5,3,8,0};
        System.out.println(func1(arr1, arr2));
        System.out.println(func2(arr1, arr2));
    }

    //并集
    private static HashSet<Integer> func2(int[] arr1, int[] arr2) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < arr1.length; i++) {
            list.add(arr1[i]);
        }
        for (int j = 0; j < arr2.length; j++) {
            list.add(arr2[j]);
        }
        HashSet<Integer> set = new HashSet<>();
        set.addAll(list);
        return set;
    }

    //交集
    private static List<Integer> func1(int[] arr1, int[] arr2) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if(arr1[i] == arr2[j]){
                    list.add(arr1[i]);
                }
            }
        }
        return list;
    }
}
